using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Jage.Utils;
using Radar;

namespace Jage.Editor.Logic
{
    public class TerrainGenerator
        : Singleton<TerrainGenerator>
    {

        /// <summary>
        /// Generates terrain array with random tile indices based on a theme.
        /// </summary>
        /// <param name="mapDefinition"></param>
        /// <param name="theme"></param>
        /// <param name="variationPercentage">Percentage of special variation tiles (0 - 100)</param>
        /// <returns></returns>
        public void FillTerrain(MapDefinition mapDefinition, Theme theme, byte variationPercentage)
        {
            if (variationPercentage < 0 || variationPercentage > 100)
            {
                throw new ArgumentOutOfRangeException(String.Format("variationIntensity ({0}) must be between 0 and 100.", variationPercentage));
            }
            else
            {
                var r = new Random(DateTime.Now.Millisecond);

                mapDefinition.Terrain.Init(mapDefinition.Width, mapDefinition.Height);
                for (int x = 0; x < mapDefinition.Width; x++)
                {
                    for (int y = 0; y < mapDefinition.Height; y++)
                    {
                        if (r.Next(101) <= variationPercentage)
                        {
                            mapDefinition.Terrain[x, y] = (byte)(100 + r.Next(theme.Terrain.GroundVariations.Count));
                        }
                        else
                        {
                            mapDefinition.Terrain[x, y] = (byte)(r.Next(theme.Terrain.Ground.Count));
                        }
                    }
                }
            }
        }

    }
}
